package com.yang.job.controller;

import com.yang.job.client.api.YangJobQueryService;
import com.yang.job.client.api.YangJobService;
import com.yang.job.client.dto.YangJobDTO;
import com.yang.job.client.dto.command.DeleteYangJobCommand;
import com.yang.job.client.dto.command.NewYangJobCommand;
import com.yang.job.client.dto.command.UpdateYangJobCommand;
import com.yang.job.client.dto.common.PageDTO;
import com.yang.job.client.dto.common.Response;
import com.yang.job.client.dto.query.PageYangJobQuery;
import com.yang.job.controller.vo.ExecuteStrategyVO;
import com.yang.job.domain.enums.JobExecuteStrategyEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/job")
public class YangJobController {
    @Autowired
    private YangJobQueryService yangJobQueryService;

    @Autowired
    private YangJobService yangJobService;

    @PostMapping(value = "/queryPage")
    public Response<PageDTO<YangJobDTO>> queryPage(@RequestBody PageYangJobQuery pageYangJobQuery) {
        return yangJobQueryService.queryYangJobPage(pageYangJobQuery);
    }

    @GetMapping(value = "/{id}")
    public Response<YangJobDTO> getYangJobById(@PathVariable("id") Integer id) {
        return yangJobQueryService.getYangJobDTOById(id);
    }

    @PostMapping
    public Response<YangJobDTO> saveYangJob(@RequestBody NewYangJobCommand newYangJobCommand) {
        return yangJobService.saveYangJob(newYangJobCommand);
    }

    @PutMapping
    public Response<YangJobDTO> updateYangJob(@RequestBody UpdateYangJobCommand updateYangJobCommand) {
        return yangJobService.updateYangJob(updateYangJobCommand);
    }

    @DeleteMapping(value = "/{id}")
    public Response deleteYangJob(@PathVariable("id") Integer id) {
        DeleteYangJobCommand deleteYangJobCommand = new DeleteYangJobCommand();
        deleteYangJobCommand.setJobId(id);
        return yangJobService.deleteYangJob(deleteYangJobCommand);
    }

    @PutMapping(value = "/open/{id}")
    public Response open(@PathVariable("id") Integer id) {
        UpdateYangJobCommand updateYangJobCommand = new UpdateYangJobCommand();
        updateYangJobCommand.setJobId(id);
        updateYangJobCommand.setOpen(1);
        return yangJobService.updateYangJob(updateYangJobCommand);
    }

    @PutMapping(value = "/close/{id}")
    public Response close(@PathVariable("id") Integer id) {
        UpdateYangJobCommand updateYangJobCommand = new UpdateYangJobCommand();
        updateYangJobCommand.setJobId(id);
        updateYangJobCommand.setOpen(0);
        return yangJobService.updateYangJob(updateYangJobCommand);
    }

    @GetMapping(value = "/executeStrategy")
    public Response<List<ExecuteStrategyVO>> getExecuteStrategyList() {
        List<ExecuteStrategyVO> executeStrategyList = new ArrayList<>();
        for (JobExecuteStrategyEnum value : JobExecuteStrategyEnum.values()) {
            ExecuteStrategyVO executeStrategyVO = new ExecuteStrategyVO();
            executeStrategyVO.setName(value.getName());
            executeStrategyVO.setDescription(value.getDescription());
            executeStrategyList.add(executeStrategyVO);
        }
        return Response.success(executeStrategyList);
    }
}
